﻿#include "TestPriceTime.h"
#include "MarketDataStream.h"
#include "QDir"
#include "MarketTimeAmendment.h"
#include "Tools.h"
#include "OptionManager.h"

TestPriceTime::TestPriceTime(const QString &pFileDir)
{
	m_fileDir = pFileDir;
}

TestPriceTime::~TestPriceTime()
{
}

void TestPriceTime::StartTest()
{
	time_t workTimeStamp = Tools::DateTimeStr2Stamp("2022-01-24 22:10:10");
	time_t errTimeStamp = Tools::DateTimeStr2Stamp("2022-01-26 00:00:00");
	MarketTimeAmendment::Instance().SetWorkTime(workTimeStamp);
	readTimeFile([errTimeStamp](const std::string &pSymbol, time_t ptime) {
		std::string ptimeStr = Tools::DateTimeStamp2Str(ptime);
		QString qstr = QString::fromLocal8Bit(ptimeStr.c_str());
		QStringList dtstrList = qstr.split(' ');
		QString pDateStr = dtstrList.at(0);
		QString pTimeStr = dtstrList.at(1);
		time_t pRealTime = MarketTimeAmendment::Instance().GetRealTime(pDateStr.toLocal8Bit().data(), pTimeStr.toLocal8Bit().data());
		if (pRealTime > errTimeStamp)
		{
			int a = 0;
		}
	});
}

void TestPriceTime::readTimeFile(std::function<void(const std::string &pSymbol, time_t ptime)> pFunc)
{
	if (pFunc == nullptr)
	{
		return;
	}
	MDIO::Price priceStream;
	std::string pDataStr = m_fileDir.toLocal8Bit().data();
	std::string filePath = OptionManager::Instance().GetMarketDataDir() + '/' + pDataStr;
	// 获取当前目录内所有合约
	QStringList pSymbolList;
	getAllSymbolNameList(QString::fromLocal8Bit(filePath.c_str()), pSymbolList);
	for (QStringList::iterator it = pSymbolList.begin(); it != pSymbolList.end(); ++it)
	{
		std::deque<MData::PriceItem> pPrices;
		priceStream.GetAllPrice(pDataStr, it->toLocal8Bit().data(), pPrices);
		for (std::deque<MData::PriceItem>::iterator priceIt = pPrices.begin(); priceIt != pPrices.end(); ++priceIt)
		{
			pFunc(it->toLocal8Bit().data(), priceIt->UpdateTime);
		}
	}
}

void TestPriceTime::getAllSymbolNameList(const QString &pDir, QStringList &outSymbolList)
{
	QDir dir(pDir);
	//要判断路径是否存在
	if (!dir.exists())
	{
		return;
	}
	dir.setFilter(QDir::Files);//实现对文件的过滤

	QFileInfoList list = dir.entryInfoList();
	QStringList string_list;

	for (int i = 0; i < list.size(); ++i)
	{
		QFileInfo fileInfo = list.at(i);
		outSymbolList << fileInfo.baseName();
	}
}
